<?php
/**
 * Created by PhpStorm.
 * User: mars
 * Date: 2019/2/11
 * Time: 1:23 AM
 */

namespace api\controllers;


use api\models\Job;
use api\models\JobRecord;
use api\models\Mission;
use api\models\search\MissionSearch;
use api\models\User;

class MissionController extends BaseController
{
    public $modelClass = "\api\models\Mission";


    public function actions()
    {
        $actions          = parent::actions();
        $actions['index'] = [
            'class'               => 'yii\rest\IndexAction',
            'modelClass'          => $this->modelClass,
            'checkAccess'         => [$this, 'checkAccess'],
            'prepareDataProvider' => function ($action, $filter) {
                $model = new MissionSearch();

                return $model->search(\Yii::$app->request->get());
            }
        ];

        return $actions;
    }

    public function actionSignUpJob()
    { //已报名的任务
        $model = Mission::find()->select(['job.*', 'job_record.uid'])
                        ->joinWith('jobInfo')
                        ->joinwith('u')
                        ->joinWith('jobRecords')
                        ->where([
                            'job_record.uid' => \Yii::$app->getUser()->id,
                            'type'           => Mission::MISSION_TYPE,
                            'forbidden'      => Mission::ACCESS,
                        ])
                        ->orderBy(['job.created_at' => SORT_DESC])
                        ->all();

        return $model;
    }

    public function actionPost()
    {
        $model = Mission::find()
                        ->joinWith('jobInfo')
                        ->joinwith('u')
                        ->where([
                            'uid'       => \Yii::$app->getUser()->id,
                            'type'      => Mission::MISSION_TYPE,
                            'forbidden' => Mission::ACCESS,
                        ])
                        ->orderBy(['job.created_at' => SORT_DESC])
                        ->all();

        return $model;
    }


    public function actionPostUser($id = null)
    { //查看自己发布的兼职报名情况
        $res = [];
        if (isset($id)) {
            $users = User::find()
                         ->select(['user.*', 'job_record.jid'])
                         ->joinWith('records')
                         ->where([
                             'job_record.jid' => $id,
                             'user.status'    => User::STATUS_ACTIVE,
                         ])->all();
//            $count = Mission::findOne($id)->view_num;
//            $count = $count == 0 ? 5 : $count;//前五个免费
            foreach ($users as $user) {
//                if ($count <= 0) {
//                    break;
//                }
//                $count--;
                array_push($res, [
                    'uid'      => $user->id,
                    'username' => $user->username,
                    'phone'    => $user->phone
                ]);
            }
        }
        return $res;

    }

    public function actionList()
    { //排行
        $res   = [];
        $users = User::find()
                     ->select([
                         'uid'         => 'user.id',
                         'username'    => 'user.username',
                         'jid'         => 'job.id',
                         'record_uid'  => 'job_record.uid',
                         'job_info.salary',
                         'total_price' => 'sum(job_info.salary)'
                     ])
                     ->rightJoin('job_record', 'user.id = job_record.uid')
                     ->leftJoin('job', 'job_record.jid = job.id')
                     ->leftJoin('job_info', 'job.id = job_info.jid')
                     ->where(['job.type' => Job::MISSION_TYPE, 'job.forbidden' => Job::ACCESS])
                     ->groupBy('user.id')
                     ->orderBy(['total_price' => SORT_DESC])
                     ->limit(10)
                     ->asArray()
                     ->all();
//        var_dump($users);

        foreach ($users as $user) {
            /**
             * @var User $user
             */
            array_push($res, [
                'id'       => $user['uid'],
                'username' => $user['username'],
                'total'    => $user['total_price']
            ]);
        }

        return $res;
    }


}